package com.yxp423.ssmempl.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxp423.ssmempl.bean.Overtime;
import com.yxp423.ssmempl.bean.User;
import com.yxp423.ssmempl.mapper.OvertimeMapper;
import com.yxp423.ssmempl.service.IOvertimeService;
import com.yxp423.ssmempl.utils.base.AjaxResult;
import com.yxp423.ssmempl.utils.consts.ApplyConst;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: java类作用描述
 * @Author: yxp
 * @CreateDate: 2019-05-03 15:31
 * @Version: 1.0
 */

@Service
public class OvertimeServiceImpl implements IOvertimeService {

    @Autowired
    private OvertimeMapper overtimeMapper;


    @Override
    public int saveOvertimeApply(Overtime overtime) {
        int flag = -1;

        overtime.setReviewStatus(ApplyConst.STATUS_CHECKING);


        try {

            flag = overtimeMapper.insert(overtime);
        } catch (Exception e) {

            return flag;
        }
        return flag;
    }

    @Override
    public IPage<Overtime> getMyAllOvertime(User user, Page page) {

        QueryWrapper<Overtime> conditions = new QueryWrapper<>();

        overtimeMapper.selectPage(
                page,
                conditions
                        .eq("user_id",user.getUserId())
                        .orderByAsc("review_status")
        );

        return page;
    }

    @Override
    public IPage<Overtime> getReviewOvertime(User user,Page page) {


        QueryWrapper<Overtime> wrapper = new QueryWrapper<>();

        try {
            overtimeMapper.selectPage(page,wrapper.
                    eq("reviewer_id",user.getUserId())
                    .eq("review_status",ApplyConst.STATUS_CHECKING)
                    .orderByDesc("creat_time"));
        } catch (Exception e) {
            e.printStackTrace();

        }

        return page;
    }

    @Override
    public AjaxResult saveReview(Overtime overtime) {


        AjaxResult result = new AjaxResult();
        Overtime overtime1 = overtimeMapper.selectById(overtime.applyId);

        if (ObjectUtil.isNull(overtime1)){

            result.setSuccess(false);
            result.setMsg("该日志不存在");
            return result;
        }

        try {
            overtime.setReviewTime(DateUtil.date());
            overtime.setVersion(overtime1.version);
            int flag = overtimeMapper.updateById(overtime);
            if (flag > 0){
                result.setSuccess(true);
                result.setMsg("保存成功，受影响的行数为"+flag);

            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("保存失败,原因为"+e.toString());

        }

        return result;
    }
}
